﻿<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>工装BOM和工艺规程同文件数据上传预览</title>
  <meta http-equiv="X-UA-Compatible" content="IE=EDGE;IE=11;IE=8,chrome=1" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link href="/InSiteWebApplication/Content/Easyui/themes/metro/easyui.css" rel="stylesheet" />
  <link href="/InSiteWebApplication/Content/Easyui/custom-easyui.css" rel="stylesheet" />
  <link href="/InSiteWebApplication/Content/Easyui/themes/icon.css" rel="stylesheet" />
  <link rel="stylesheet" href="/InSiteWebApplication/Content/webuploader/webuploader.css" />
  <style type="text/css">
    .webuploader-pick {
      position: relative;
      display: inline-block;
      cursor: pointer;
      background: #00b7ee;
      padding: 5px 15px;
      color: #fff;
      text-align: center;
      overflow: hidden;
      font-size: 15px;
    }

    #temUploader .webuploader-pick {
      width: 200px;
    }
  </style>
</head>

<body>
  <div id="divMain">
    <div class="panelSection">
      <div class="easyui-panel " title="导入结果" style="padding: 3px;"
        data-options="closable:false,collapsible:true,minimizable:false,maximizable:false">
        <div style="background: #fff; margin: 3px 0px 3px 3px">
          <div style="width: 1100px">
            <table id="gdRecordInfo" class="easyui-datagrid" style="width: 100%; height: 350px"
              data-options="rownumbers:true,singleSelect:true">
            </table>
          </div>
          <div style="clear: both">
          </div>
        </div>
      </div>
    </div>
    <div class="panelSection">
      <div class="easyui-panel " title="导入结果" style="padding: 3px;"
        data-options="closable:false,collapsible:true,minimizable:false,maximizable:false">
        <div style="float: left">
          <div class="divLabel" style="margin-bottom: 5px">
            工区：</div>
          <select style='width: 200px; height: 20px' id='selWorkCenter' data-options='editable:false'
            class='easyui-combobox'>
          </select>
        </div>
        <div style="clear: both">
        </div>
      </div>
    </div>
    <div class="ufcSectionStyle">
      <span class="ufcSectionSpanStyle">导入区域</span>
    </div>
    <div style="margin: 3px 0px 3px 3px">
      <div class="divLabel">
        选择导入Excel文件：</div>
      <div id="uploader" style="margin-top: 5px" class="wu-example">
        <div style="float: left">
          <input id="txtExcelFileName" class="easyui-textbox" type="text" style="width: 200px;
                        height: 22px; float: left;" readonly="readonly" />
          <input id="txtExcelFileId" type="hidden" />
        </div>
        <div style="float: left" class="btns">
          <div id="picker" style="float: left; margin-left: 0px; margin-top: 0px;">
            选择文件</div>
          <span style="color:red;font-weight:bold;margin-left: 10px;">内部系统，禁止处理涉密信息！</span>
          <button id="ctlBtn" class="easyui-linkbutton" style="float: left; margin-left: 5px;
                        margin-top: -2px; height: 26px; width: 80px;">
            预览</button>
        </div>
      </div>
    </div>
    <div style="clear: both">
    </div>
    <div id="statusMessage" style="font-size: 14px; height: 40px">
    </div>
  </div>
  <div id="divDownload">
    <a href="" id="gzdownload"></a>
  </div>
  <div id="dgWF">
  </div>
</body>

</html>
<script src="/InSiteWebApplication/Content/Easyui/jquery.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Scripts/json2.js" type="text/javascript"></script>
<script type="text/javascript" src="/InSiteWebApplication/Content/webuploader/webuploader.js"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.easyui.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/easyui-lang-zh_CN.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/CustomCommon.js" type="text/javascript"></script>
<script type="text/javascript">
  var page = {
    rootPath: getRootPath(), //网站根路径
    init: function () {
      page.bind();
      page.bindUpload();
      page.bindWorkCenter();
    },
    bind: function () {
      //列表加载
      $("#gdRecordInfo").datagrid({
        columns: [[
          { field: 'sheetName', title: '工作薄名', width: 280 },
          {
            field: 'sheetIndex', title: '预览', width: 120,
            formatter: function (value, row, index) {
              return "<button class='easyui-linkbutton' onclick='page.viewAndSave(" + value + ",\"" + row.fileName + "\")' style='width: 100px; height: 22px'>预览保存</button>";
            }
          },
          {
            field: 'sheetIndex1', title: '保存', width: 120,
            formatter: function (value, row, index) {
              return "<button class='easyui-linkbutton' onclick='page.save(" + value + ",\"" + row.fileName + "\")' style='width: 100px; height: 22px'>保存</button>";
            }
          },
          {
            field: 'download', title: '下载', width: 120,
            formatter: function (value, row, index) {
              return "<button class='easyui-linkbutton' onclick='page.download(" + row.sheetIndex + ",\"" + row.sheetName + "\",\"" + row.fileName + "\")' style='width: 100px; height: 22px'>下载</button>";
            }
          },
          { field: 'remark', title: '备注', width: 200 },
          { field: 'fileName', width: 200, hidden: true }
        ]]
      });
      //保存按钮
      $("#btnSave").on("click", function () {
        page.saveData();
      })
    },
    //初始化绑定工区
    bindWorkCenter: function () {
      $.ajax({
        type: "Post",
        url: page.rootPath + '/Ashx/GongZhuangContainer/GZBaseDataHandler.ashx',
        data: { action: "GetWorkCenterList" },
        cashe: false,
        beforeSend: function () {
          $.messager.progress({ text: '', msg: '处理中...' });
        },
        success: function (data) {
          $.messager.progress('close');
          var result = JSON.parse(data);
          if (result.Result) {
            //绑定工区
            $("#selWorkCenter").combobox({
              valueField: 'WORKCENTERID',
              textField: 'WORKCENTERNAME',
              panelHeight: 200,
              data: result.DBTable //下拉数据
            });
          }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $.messager.progress('close');
          displayMessage(XMLHttpRequest.responseText, false);
        }
      });
    },
    //上传数据
    bindUpload: function () {
      var uploader = WebUploader.create({
        // swf文件路径
        swf: page.rootPath + "/Content/webuploader/Uploader.swf",
        // 文件接收服务端。
        server: page.rootPath + '/Ashx/GongZhuangContainer/GZBaseDataHandler.ashx',
        // 内部根据当前运行是创建，可能是input元素，也可能是flash.
        pick: '#picker',
        // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
        resize: false,
        formData: {
          action: "UploadGZBaseExcel"
        },
        accept: {
          title: 'Excel',
          extensions: 'xlsx,xls',
          mimeTypes: 'excel/*'
        },
        multiple: false
      });
      //上传
      $('#ctlBtn').on("click", function () {
        var filename = $('#txtExcelFileName').textbox('getValue');
        if (filename == "") {
          displayMessage("未选择文件", false);
        } else {
          $('#ctlBtn').attr("disabled", "disabled"); //禁用按钮
          $.messager.progress({ text: '', msg: '处理中...' });
          uploader.upload();
        }
      });
      // 当有文件被添加进队列的时候，每次只传一个文件
      uploader.on('fileQueued', function (file) {
        if ($('#txtExcelFileId').val() != "") {
          var f = uploader.getFile($('#txtExcelFileId').val()); //找到上一次队列中的文件
          uploader.removeFile(f, true); //删除掉上一次队列中的文件
        }
        $('#txtExcelFileName').textbox('setValue', file.name);
        $('#txtExcelFileId').val(file.id);
      });
      //上传成功
      uploader.on('uploadSuccess', function (file, response) {
        displayMessage("", true);
        $.messager.progress('close');
        var result = response;
        if (result.Result) {
          $("#gdRecordInfo").datagrid('loadData', { rows: result.DBTable });
        }
        displayMessage(result.Message, result.Result);
        var f = uploader.getFile(file.id); //找到上传的文件
        uploader.removeFile(f, true); //将其从队列中的删掉
        $('#txtExcelFileName').textbox('setValue', "");
        $('#txtExcelFileId').val("");
        $('#ctlBtn').removeAttr("disabled"); //恢复按钮
      });
      //上传失败
      uploader.on('uploadError', function (file) {
        $.messager.progress('close');
        displayMessage("上传失败", false);
        var f = uploader.getFile(file.id); //找到上传的文件
        uploader.removeFile(f, true); //将其从队列中的删掉
        $('#txtExcelFileName').textbox('setValue', "");
        $('#txtExcelFileId').val("");
        $('#ctlBtn').removeAttr("disabled"); //恢复按钮
      });
    },
    //预览并保存
    viewAndSave: function (sheetIndex, fname) {
      clearMessage();
      if (sheetIndex === undefined || sheetIndex === null || sheetIndex === "") {
        displayMessage("该文件解析出错", false);
        return;
      }
      var addr = "GZWorkflowUploadPage.html"; //工艺规程上传
      var workcenterID = "";
      var workcenterName = "";
      if (sheetIndex === 0) {
        addr = "GZProductBomUploadPage.html"; //BOM产品上传
      } else {
        workcenterID = $("#selWorkCenter").combobox("getValue");
        workcenterName = $("#selWorkCenter").combobox("getText");
        if (isNullOrWhiteSpaceStr(workcenterID)) {
          displayMessage2("没有选择工区", false, ["btnSave"]);
          return;
        }
      }
      var pupDatas = {
        findex: sheetIndex,
        filename: fname,
        workcenter: [{ WORKCENTERID: workcenterID, WORKCENTERNAME: workcenterName }]
      }
      //打开弹出页面
      var url = page.rootPath + '/forms/Custom/GongZhuang/' + addr + "?v=" + new Date().getTime();
      url += "&parentDiv=dgWF";
      $('#dgWF').dialog({
        title: '工装数据预览保存',
        width: 1200,
        height: 800,
        top: 10,
        closed: false,
        cache: false,
        modal: true,
        content: '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="auto"></iframe>',
        poPupData: pupDatas, //弹出页面的传参
        onClose: function () {
          var returnValue = window.returnValue;
          if (returnValue != null && returnValue != undefined) {
            var row = $("#gdRecordInfo").datagrid('getSelected');
            var rowindex = $("#gdRecordInfo").datagrid('getRowIndex', row);
            $("#gdRecordInfo").datagrid('updateRow', { index: rowindex, row: { remark: returnValue.message } });
            $("#gdRecordInfo").datagrid("acceptChanges");
          }
        }
      });
    },
    //直接保存
    save: function (sheetIndex, fname) {
      clearMessage();
      if (sheetIndex === undefined || sheetIndex === null || sheetIndex === "") {
        displayMessage("该文件解析出错", false);
        return;
      }
      var action = "UploadWorkflowExcel"; //工艺规程上传
      if (sheetIndex === 0) {
        var formDatas = {
          fileName: fname,
          action: "ViewProductBomExcel" //BOM产品预览
        }
        page.getBomData(formDatas);
      } else {
        var workcenterID = $("#selWorkCenter").combobox("getValue");
        var workcenterName = $("#selWorkCenter").combobox("getText");
        if (isNullOrWhiteSpaceStr(workcenterID)) {
          displayMessage2("没有选择工区", false, ["btnSave"]);
          return;
        }
        var formDatas = {
          action: "ViewWorkflowExcel",
          fileIndex: sheetIndex,
          fileName: fname,
          workcenterName: workcenterName,
          workcenterID: workcenterID
        }
        page.getWorkFlowData(formDatas);
      }

    },
    //获取解析后的BOM
    getBomData: function (formDatas) {
      $.ajax({
        type: "Post",
        url: page.rootPath + '/Ashx/GongZhuangContainer/GZBaseDataHandler.ashx',
        data: formDatas,
        cashe: false,
        beforeSend: function () {
          $.messager.progress({ text: '', msg: '处理中...' });
        },
        success: function (data) {
          $.messager.progress('close');
          var result = JSON.parse(data);
          if (result.Result) {
            var resDatas = result.DBTable;
            if (result.ReturnData) {
              if (!confirm("BOM已存在，是升级版本，否更新版本")) { //如果更新版本，则将版本减1
                for (var i = 0; i < resDatas.length; i++) {
                  if (resDatas[i].ProductRev !== "1") {
                    resDatas[i].ProductRev = parseInt(resDatas[i].ProductRev, 10) - 1;
                  }
                  if (resDatas[i].BomRev !== "1") {
                    resDatas[i].BomRev = parseInt(resDatas[i].BomRev, 10) - 1;
                  }
                }
              }
            }
            page.saveBomData(resDatas);
          }
          else {
            page.showCellMsg(result.Message);
          }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $.messager.progress('close');
          page.showCellMsg(XMLHttpRequest.responseText);
        }
      });
    },
    //保存BOM
    saveBomData: function (datas) {
      $.ajax({
        type: "Post",
        url: page.rootPath + '/Ashx/GongZhuangContainer/GZBaseDataHandler.ashx',
        data: { action: "SaveProductBomDatas", recordDt: JSON.stringify(datas) },
        cashe: false,
        beforeSend: function () {
          $.messager.progress({ text: '', msg: '处理中...' });
        },
        success: function (data) {
          $.messager.progress('close');
          var result = JSON.parse(data);
          page.showCellMsg(result.Message);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $.messager.progress('close');
          page.showCellMsg(XMLHttpRequest.responseText);
        }
      });
    },
    //获取解析后的工艺路线
    getWorkFlowData: function (formDatas) {
      $.ajax({
        type: "Post",
        url: page.rootPath + '/Ashx/GongZhuangContainer/GZBaseDataHandler.ashx',
        data: formDatas,
        cashe: false,
        beforeSend: function () {
          $.messager.progress({ text: '', msg: '处理中...' });
        },
        success: function (data) {
          $.messager.progress('close');
          var result = JSON.parse(data);
          if (result.Result) {
            if (result.ReturnData) {
              if (!confirm("工艺已存在，是否升级工艺版本？")) {
                return;
              }
            }
        page.saveWorkFlowData(result.DBTable, formDatas.workcenterName, formDatas.workcenterID);
          }
          else {
            page.showCellMsg(result.Message);
          }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $.messager.progress('close');
          page.showCellMsg(XMLHttpRequest.responseText);
          displayMessage(XMLHttpRequest.responseText, false);
        }
      });
    },
    //保存工艺路线
saveWorkFlowData: function (datas, workCenterName, workcenterID) {
      $.ajax({
        type: "Post",
        url: page.rootPath + '/Ashx/GongZhuangContainer/GZBaseDataHandler.ashx',
        data: { action: "SaveWorkflowDatas", recordDt: JSON.stringify(datas), workCenterName: workCenterName, workcenterID: workcenterID },
        cashe: false,
        beforeSend: function () {
          $.messager.progress({ text: '', msg: '处理中...' });
        },
        success: function (data) {
          $.messager.progress('close');
          var result = JSON.parse(data);
          page.showCellMsg(result.Message);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $.messager.progress('close');
          page.showCellMsg(XMLHttpRequest.responseText);
        }
      });
    },
    //下载
    download: function (index, sheetName, fname) {
      $.ajax({
        type: "Post",
        url: page.rootPath + '/Ashx/GongZhuangContainer/GZBaseDataHandler.ashx',
        data: { action: "DownloadGZSheetFile", fileName: fname, sheetName: sheetName, fileIndex: index },
        cashe: false,
        beforeSend: function () {
          $.messager.progress({ text: '', msg: '处理中...' });
        },
        success: function (data) {
          $.messager.progress('close');
          var result = JSON.parse(data);
          if (result.Result) {
            $("#gzdownload").attr("href", page.rootPath + "/Ashx/GongZhuangContainer/" + result.ReturnData);
            $("#gzdownload")[0].click();
          }
          displayMessage(result.Message, result.Result);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $.messager.progress('close');
          displayMessage(XMLHttpRequest.responseText, false);
        }
      });
    },
    //单元格显示提示消息
    showCellMsg: function (msg) {
      var row = $("#gdRecordInfo").datagrid('getSelected');
      var rowindex = $("#gdRecordInfo").datagrid('getRowIndex', row);
      $("#gdRecordInfo").datagrid('updateRow', { index: rowindex, row: { remark: msg } });
      $("#gdRecordInfo").datagrid("acceptChanges");
    }
  };
  page.init();
  $(document).ready(function () {
    $('.webuploader-pick').css("padding", "3.5px 5px"); //改变上传按钮样式
  });
</script>